<?php


use Phinx\Seed\AbstractSeed;
use Faker\Factory as FakerFactory;
use App\Library\Utils\Arr;

class ArticleSeeder extends AbstractSeed
{
    public function getDependencies(): array
    {
        return [
            'CategorySeeder',
        ];
    }

    /**
     * Run Method.
     *
     * Write your database seeder using this method.
     *
     * More information on writing seeders is available here:
     * https://book.cakephp.org/phinx/0/en/seeding.html
     */
    public function run()
    {
        $faker = FakerFactory::create('zh_CN');

        $categories = $this->fetchAll("SELECT * FROM kg_category");
        $users = $this->fetchAll('SELECT * FROM kg_user');

        $rows = [];
        for ($i = 0; $i < 1000; $i++) {
            $rows[] = [
                'title' => $faker->text(25),
                'cover' => $faker->imageUrl(150, 80, 'nature', true),
                'summary' => $faker->text(50),
                'tags' => '[]',
                'content' => $faker->text,
                'score' => 0,
                'category_id' => Arr::rand($categories)['id'],
                'owner_id' => Arr::rand($users)['id'],
                'source_type' => 1,
                'client_type' => 0,
                'client_ip' => $faker->ipv4,
                'private' => 0,
                'closed' => 0,
                'featured' => !($i % 10),
                'published' => rand(1, 2),
                'create_time' => $faker->unixTime,
                'update_time' => $faker->unixTime,
            ];
        }
        $this->table('kg_article')->insert($rows)->saveData();
    }

}
